<?php


class ModelePDO {
    
    protected static $serveur = MySqlConfig::SERVEUR;
    protected static $base = MySqlConfig::BASE;
    protected static $utilisateur = MySqlConfig::UTILISATEUR;
    protected static $passe = MySqlConfig::MOT_DE_PASSE;
    
    
    
    /**
     *
     * @var PDO
     */
    protected static $pdoCnxBase = null;
    
    /**
     *
     * @var PDOStatement
     */
    protected static $pdoStResults = null;
    
    protected static $requete = "";
    protected static $resultat = null;
    
    
    
    protected static function seConnecter(){
        if(is_null(self::$pdoCnxBase)){
            try{
                self::$pdoCnxBase = new PDO('mysql:host='.self::$serveur.';dbname='.self::$base,self::$utilisateur,self::$passe);
                self::$pdoCnxBase->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
                self::$pdoCnxBase->query("SET CHARACTER SET utf8");
            } catch (Exception $e) {
                echo $e->getMessage();

            } 
        }
       
    }
    protected static function seDeconnecter(){
        self::$pdoCnxBase = null;
        
    }
    protected static function getLesTuples($table){
        self::seConnecter();
        
        self::$requete = "SELECT* FROM ".$table."";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);
        self::$pdoStResults->closeCursor();
        
        return self::$resultat;
                
    }
        protected static function getCount($table,$nomChamp1){
        self::seConnecter();
        self::$requete = "select COUNT(" .$nomChamp1. ") AS re from ".$table."";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        self::$resultat = self::$pdoStResults->fetch(PDO::FETCH_OBJ)->re;
        self::$pdoStResults->closeCursor();
         return self::$resultat;
    }
     protected static function getByPage($table,$premiereEntree,$messagesParPage){
        self::seConnecter();
        self::$requete = "select * from ".$table." ORDER BY idproduit DESC LIMIT $premiereEntree, $messagesParPage";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);
        self::$pdoStResults->closeCursor();
        return self::$resultat;
    }
         protected static function getTuplesByChamp($table,$nomChamp1){
        self::seConnecter();
         self::$requete = "SELECT * FROM ".$table." order by ".$nomChamp1."";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);
        self::$pdoStResults->closeCursor();
        
        return self::$resultat;
    }
    
    
    
    
}
